/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package services;

//import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import common.Configuration;

import models.SurveyDTO;

/**
 *
 * @author User
 */
public class SurveyServiceImpl implements IEntity{
	
	Statement stmt=null;
    ResultSet rst=null;
    private SurveyDTO survey;
    private List<SurveyDTO> surveyList;

    public SurveyDTO GetById(int id) {
    	
        try {
        	
            stmt = Configuration.GetStatement();
            rst = stmt.executeQuery("select * from survey where idSurvey=" + id); 
            survey.setidSurvey(id);
            survey.setSurveyTitle(rst.getString("SurveyTitle"));
            survey.setSurveyDesc(rst.getString("SurveyDesc"));
            survey.setStartDate(rst.getDate("StartDate"));
            survey.setEndDate(rst.getDate("EndDate"));
            survey.setIdUser(rst.getInt("idUser"));
            survey.setIsActive(rst.getBoolean("IsActive"));
            Configuration.CloseConnection();
            
        } catch (SQLException ex) {
            Logger.getLogger(SurveyServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return survey;
    }

    public List<SurveyDTO> GetAll() {
        
    	try {
        	
            stmt = Configuration.GetStatement();
            rst = stmt.executeQuery("select * from survey ");
            while (rst.next()) {
                survey.setidSurvey(rst.getInt("idSurvey"));
                survey.setSurveyTitle(rst.getString("SurveyTitle"));
                survey.setSurveyDesc(rst.getString("SurveyDesc"));
                survey.setStartDate(rst.getDate("StartDate"));
                survey.setEndDate(rst.getDate("EndDate"));
                survey.setIdUser(rst.getInt("idUser"));
                survey.setIsActive(rst.getBoolean("IsActive"));
                surveyList.add(survey);
            }
            Configuration.CloseConnection();
            
          } catch (SQLException ex) {
            Logger.getLogger(SurveyServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
      return  surveyList;
    }

    public void Add(Object obj){

        try {
        	
        	SurveyDTO objCast=(SurveyDTO)obj;
        	stmt =  Configuration.GetStatement();
        	stmt.execute("INSERT INTO dbsurvey.survey (`SurveyTitle`, `SurveyDesc`, `StartDate`, `EndDate`, `idUser`, `IsActive`)   VALUES ("+objCast.getSurveyTitle()+","+objCast.getSurveyDesc()+","+objCast.getStartDate()+","+objCast.getEndDate()+","+objCast.getIdUser()+","+objCast.getIsActive()+")");
			Configuration.CloseConnection();
        
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }

    public void Update(Object obj) {
    	
        try {
    	
        	SurveyDTO objCast=(SurveyDTO)obj;
        	stmt = Configuration.GetStatement();
        	stmt.execute("update dbsurvey.survey set  SurveyTitle="+objCast.getSurveyTitle()+"and SurveyDesc="+objCast.getSurveyDesc()+"StartDate="+objCast.getStartDate()+"EndDate="+objCast.getStartDate()+"idUser="+objCast.getIdUser()+"IsActive="+objCast.getIsActive()+" where idSurvey="+objCast.getidSurvey());
        	Configuration.CloseConnection();
      
  	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
    }

    public void Remove(int id) {
    	
        try {
        	
            stmt = Configuration.GetStatement();
			stmt.execute("delete from survey where idSurvey =" + id);
			Configuration.CloseConnection();
        
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }

}
